<template>
    <div class="table-page">
        <div class="table-search-box" @keyup.enter="search()">
            <div class="search-item">
                <label>key值：</label>
                <div class="table-query-input">
                    <el-input
                        v-model="queryData.key"
                        clearable
                        placeholder="key值"
                    ></el-input>
                </div>
            </div>
            <div class="search-item">
                <label>类型：</label>
                <div class="table-query-input">
                    <el-select
                        v-model="queryData.type"
                        filterable
                        clearable
                        placeholder="状态"
                        class="address-select"
                    >
                        <el-option
                            v-for="item in configTypeList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        >
                        </el-option>
                    </el-select>
                </div>
            </div>

            <div class="search-item">
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    :loading="tableLoading"
                    @click="clickSearch()"
                    >搜索
                </el-button>
            </div>
            <div class="search-item">
                <el-button
                    icon="el-icon-refresh"
                    :loading="tableLoading"
                    @click="resetSearch()"
                    >重置
                </el-button>
            </div>
        </div>
        <div class="table-operation-box">
            <el-button
                type="primary"
                style="margin-bottom: 2em"
                icon="el-icon-plus"
                @click="clickAdd('新增')"
                >添加
            </el-button>
            <el-button
                type="danger"
                style="margin-bottom: 2em"
                icon="el-icon-delete"
                :disabled="selectDisable"
                @click="
                    tableDelete(
                        '/user/sys/config/deletes',
                        {
                            ids: tableSelect
                        },
                        true
                    )
                "
                >批量删除
            </el-button>
        </div>
        <div class="table-data-box">
            <el-table
                ref="table"
                v-loading="tableLoading"
                :data="tableData"
                border
                style="width: 100%;min-height:40em"
                row-key="id"
                @selection-change="tableSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center">
                </el-table-column>
                <el-table-column
                    prop="key"
                    label="key"
                    min-width="100"
                ></el-table-column>
                <el-table-column
                    prop="value"
                    label="内容"
                >
                    <template slot-scope="scope">
                        <el-input
                            v-model="scope.row.value"
                            type="textarea"
                            autosize >

                        </el-input>
                    </template>
                </el-table-column>
                <el-table-column prop="remark" label="备注"></el-table-column>
                <el-table-column prop="type" label="类型" width="120">
                    <template slot-scope="scope">
                        <k-tag
                            :tag="configType[scope.row.type]"
                            :text="true"
                        ></k-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="300" align="center">
                    <template slot-scope="scope">
                        <el-button
                            v-if="!scope.row.edit"
                            class="primary"
                            type="text"
                            size="mini"
                            icon="el-icon-edit"
                            @click="lineEdit(scope.row)"
                            >保存
                        </el-button>
                        <el-button
                            v-if="!scope.row.edit"
                            type="text"
                            size="mini"
                            class="warning"
                            icon="el-icon-delete"
                            :loading="scope.row.opLoading"
                            @click="
                                tableDelete(
                                    '/user/sys/config/delete',
                                    {
                                        id: scope.row.id
                                    },
                                    true
                                )
                            "
                            >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div v-if="pageData.total" class="xy-between table-page-box">
            <el-pagination
                background
                :current-page.sync="pageData.currentPage"
                :page-sizes="pageData.pageSizes"
                :page-size.sync="pageData.pageSize"
                layout="total,  prev, pager, next, sizes, jumper"
                :total="pageData.total"
            >
            </el-pagination>
        </div>

        <el-dialog
            :title="dialogFormTitle"
            :visible.sync="dialogFormVisible"
            :before-close="dialogClose"
            custom-class="two-dialog"
        >
            <el-form
                ref="dataForm"
                :model="dataForm"
                label-width="6em"
                :rules="dataFormRule"
                class="table-edit-from"
            >
                <el-form-item label="类型" prop="type">
                    <el-select v-model="dataForm.type" placeholder="请选择">
                        <el-option
                            v-for="item in configTypeList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="key" prop="key">
                    <el-input v-model="dataForm.key"></el-input>
                </el-form-item>
                <el-form-item label="内容" prop="value">
                    <el-input
                        v-model="dataForm.value"
                        type="textarea"
                        :rows="4"
                    ></el-input>
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input
                        v-model="dataForm.remark"
                        type="textarea"
                        :rows="4"
                    ></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button :loading="submitFlag" @click="dialogClose"
                    >取 消</el-button
                >
                <el-button
                    :loading="submitFlag"
                    type="primary"
                    @click="contentChange()"
                    >保 存</el-button
                >
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { TablePage } from "@/layout/mixin/iVue.js";
import { mapState } from "vuex";
import {post, put} from "@/api/request";
import message from "element-ui/packages/message";

export default {
    name: "ConfigList",
    components: {},
    mixins: [TablePage],
    data() {
        return {
            tabClickIndex: null,
            tabClickLabel: "",
            configTypeList: [{ label: "系统配置", value: "system" }],
            queryData: {
                key: "",
                type: null,
            },
            dataForm: {
                id: null,
                remark: null,
                content: {}
            },
            dataFormRule: {
                key: [
                    {
                        required: true,
                        message: "请输入key",
                        trigger: "blur"
                    }
                ],
                type: [
                    {
                        required: true,
                        message: "请选择类型",
                        trigger: "change"
                    }
                ]
            }
        };
    },
    computed: {
        ...mapState({
            configType: state => state.status.configType
        })
    },
    watch: {},
    created() {},
    mounted() {},
    methods: {
        search(queryData) {
            this.loadTable("/user/sys/config/page", queryData);
        },
        contentChange(r) {
            this.formDataSave("/user/sys/config/update");
        },
        lineEdit(data){
            this.dataForm = data;
            put("/user/sys/config/update", {
                ...data
            }).then(res => {
                return this.$message.success("保存成功");
            }).catch(res => {
                return this.$message.error(res.message);
            })
        }
    }
};
</script>

<style lang="scss" scoped>
.school-logo {
    .logo-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 108px;
        height: 108px;
        line-height: 108px;
        text-align: center;
    }

    .logo-img {
        width: 108px;
        height: 108px;
        display: block;
    }
}

.table-edit-from {
    .address-select {
        width: 8em;
        margin-right: 2em;
    }
}
</style>
<style>
.school-logo .logo-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.school-logo .logo-uploader .el-upload:hover {
    border-color: #409eff;
}
</style>
